﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WeaponUpdate : MonoBehaviour {
	List<WeaponTrail> trails;

	static WeaponUpdate _instance = null;
	public static WeaponUpdate Instance {
		get {
			if (_instance == null || !_instance.gameObject) {
				GameObject g = new GameObject ("__WeaponUpdate");
				_instance = g.AddComponent <WeaponUpdate> ();
				_instance.InitScript ();
			}
			return _instance;
		}
	}

	void InitScript () {
		trails = new List<WeaponTrail> ();
	}

	/// <summary>
	/// Lates the update.
	/// </summary>
	void LateUpdate () {
		UpdateTrails ();
	}

	void UpdateTrails () {
		for (int j = 0; j < trails.Count; j++) {
			if (trails[j].time > 0) {
				trails[j].Itterate (Time.time - Time.deltaTime);
			} else {
				trails[j].ClearTrail ();
			}
		}
		
		for (int j = 0; j < trails.Count; j++) {
			if (trails[j].time > 0) {
				trails[j].UpdateTrail (Time.time, Time.deltaTime);
			}
		}
	}

	/// <summary>
	/// Adds the trail.
	/// </summary>
	/// <param name="trail">Trail.</param>
	public void AddTrail (WeaponTrail trail) {
		trails.Add (trail);
	}
}
